<template>
  <div>
    <el-button type="primary" @click="handleCreate"> 新建 </el-button>

    <el-table
      ref="table"
      class="mt10"
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      current-row-key="id"
      stripe
      border
      highlight-current-row
    >
      <el-table-column prop="code" label="触发器编码" />
      <el-table-column prop="name" label="触发器名称" />
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === 0 ? "禁用" : "启用" }}
        </template>
      </el-table-column>
      <el-table-column prop="relBusinessMethods" label="关联业务方法">
        <template slot-scope="scope">
          {{ scope.row.services.map((i) => i.name).join() }}
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" />
      <el-table-column prop="createDate" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.createDate | moment }}
        </template>
      </el-table-column>
      <el-table-column prop="modifyBy" label="更新人" />
      <el-table-column prop="modifyDate" label="更新时间">
        <template slot-scope="scope">
          {{ scope.row.modifyDate | moment }}
        </template>
      </el-table-column>
      <el-table-column prop="actions" label="操作" width="180">
        <template slot-scope="scope">
          <span class="table-actionItem">
            <el-button type="text" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
          </span>

          <span class="table-actionItem">
            <el-popconfirm
              title="确认要删除该触发器，删除后无法恢复？"
              placement="top"
              @confirm="handleDelete(scope.row)"
            >
              <el-button class="link-danger" type="text" slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </span>
          <span v-if="scope.row.status === 0" class="table-actionItem">
            <el-popconfirm
              title="确定要启用触发器吗？"
              placement="top"
              @confirm="changeStatus(scope.row, 1)"
            >
              <el-button type="text" slot="reference">启用</el-button>
            </el-popconfirm>
          </span>
          <span v-if="scope.row.status !== 0" class="table-actionItem">
            <el-popconfirm
              title="禁用后关联业务方法调用后也不会触发该触发器，确定要禁用该触发器吗？"
              placement="top"
              @confirm="changeStatus(scope.row, 0)"
            >
              <el-button class="link-danger" type="text" slot="reference"
                >禁用</el-button
              >
            </el-popconfirm>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <TriggerForm ref="TriggerForm" :modelInfo="modelInfo" @ok="loadData" />
  </div>
</template>

<script>
import {
  getHmtriggerListbymodel,
  deleteHmtrigger,
  putHmtriggerDisableById,
  putHmtriggerEnableById,
} from "@/apis/paas/HmTriggerController.js"
import TriggerForm from "./TriggerForm"

export default {
  name: "RuleList",
  components: {
    TriggerForm,
  },

  props: {
    modelInfo: {
      type: Object,
      default: () => {},
    },
  },

  data() {
    return {
      loading: false,
      tableData: [],
    }
  },

  methods: {
    init() {
      this.loadData()
    },

    loadData() {
      getHmtriggerListbymodel({
        modelId: this.modelInfo.id,
      }).then((res) => {
        this.tableData = res
      })
    },
    handleCreate() {
      this.$refs.TriggerForm.addForm()
    },

    handleEdit(record) {
      this.$refs.TriggerForm.editForm({
        ...record,
      })
    },

    handleDelete(record) {
      deleteHmtrigger([record.id]).then((res) => {
        this.loadData()
        this.$message.success("操作成功")
      })
    },
    changeStatus(record, flag) {
      let api = putHmtriggerDisableById
      if (flag === 1) {
        api = putHmtriggerEnableById
      }
      api({ id: record.id }).then(() => {
        this.loadData()
        this.$message.success("操作成功")
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>
